package com.sanpu.kxe.business.main.view;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.business.main.contract.PartnerContract;
import com.sanpu.kxe.business.main.presenter.PartnerPresenter;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.business.trade.provider.ITradeProvider;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.common.view.X5WebView;
import com.sanpu.kxe.utils.PublicProviderUtil;
import com.tencent.smtt.export.external.interfaces.ConsoleMessage;
import com.tencent.smtt.export.external.interfaces.JsPromptResult;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description：我的合伙人
 * Created by 薛勇军 on 2018/7/10.
 */
@Route(path = "/hotRecommend/PartnerAct")
public class PartnerActivity extends BaseTitleActivity implements PartnerContract.View {

    @Bind(R.id.webViewLayout)
    FrameLayout  vWebViewLayout;
    @Bind(R.id.partnerLayout)
    LinearLayout vPartnerLayout;
    @Bind(R.id.partnerView)
    View vPartnerView;
    @Bind(R.id.refCountTv)
    TextView vRefCount;
    @Bind(R.id.rebCountTv)
    TextView vRebCount;
    @Bind(R.id.partnerRefCountTv)
    TextView vPartnerRefCount;
    @Bind(R.id.dayCountTv)
    TextView vDayCount;
    @Bind(R.id.reservedLayout)
    LinearLayout vReservedLayout;
    @Bind(R.id.uncashAmount)
    TextView vUncashAmount;

    private X5WebView vWebView;

    private PartnerContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_partner;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("合伙人");
        commonTitle.showRightTextView("收益说明");

        initWebView();
        mPresenter = new PartnerPresenter(this);
        mPresenter.showViewData();
    }

    @Override
    protected void setListener() {
        super.setListener();
        commonTitle.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainOpenProvider.getInstance().openBrowserX5Activity(mContext, UrlConstants.PARTNER_REMARK, false);
            }
        });
    }

    @OnClick({R.id.reb_layout, R.id.ref_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.reb_layout:
                ITradeProvider provider = PublicProviderUtil.getProvider(ITradeProvider.class);
                if (provider != null) {
                    provider.openMyRebTotalActivity(mContext, 1);
                }
                break;
            case R.id.ref_layout:
                MainOpenProvider.getInstance().openPartnerTeamListActivity(mContext);
                break;
        }
    }

    /**
     * 初始化webview
     */
    private void initWebView() {
        vWebView = new X5WebView(mContext, null);
        vWebViewLayout.addView(vWebView, new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT));
        vWebView.getSettings().setJavaScriptEnabled(true);
        WebView.setWebContentsDebuggingEnabled(true);
        vWebView.loadUrl(UrlConstants.PARTNER_NOTSIGN);

        vWebView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

            }
        });
        vWebView.setWebChromeClient(new WebChromeClient() {

            @Override
            public boolean onJsPrompt(WebView webView, String url, String message, String s2,
                                      JsPromptResult result) {
                return true;
            }

            @Override
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                return true;
            }
        });
    }

    @Override
    public void showPartnerView(boolean isPartner) {
        if (isPartner) {
            vWebViewLayout.setVisibility(View.GONE);
            vPartnerLayout.setVisibility(View.VISIBLE);
        } else {
            vWebViewLayout.setVisibility(View.VISIBLE);
            vPartnerLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void showPartnerLvl(String partnerLvl) {
        switch (partnerLvl) {
            case MainConfig.PARTNER_GOLD:
                commonTitle.setTitle("金牌合伙人");
                break;
            case MainConfig.PARTNER_SILVER:
                commonTitle.setTitle("银牌合伙人");
                break;
            case MainConfig.PARTNER_BRONZE:
                commonTitle.setTitle("铜牌合伙人");
                break;
        }
    }

    @Override
    public void showReservedView(boolean isReserved, String uncashAmount) {
        if (isReserved) {
            vReservedLayout.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(uncashAmount))
                vUncashAmount.setText(uncashAmount);
        } else {
            vReservedLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void showPartnerData(int allNum, int undRefNum, String undDayReb, String undAccReb) {
        vRefCount.setText(String.valueOf(allNum));
        vPartnerRefCount.setText(String.valueOf(undRefNum));
        if (!TextUtils.isEmpty(undDayReb)) {
            vDayCount.setText(undDayReb);
        }
        if (!TextUtils.isEmpty(undAccReb)) {
            vRebCount.setText(undAccReb);
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(PartnerContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
